<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class OwnserviceControllerAccount extends JController
{
	/**
	 * Controller construct
	 *
	*/
	function __construct(){
		parent::__construct();
	}

	/**
	 * Method to get detail customer account
	 *
	 * @return void
	 * @since 1.0
	*/
	function detail() {

		global $mainframe;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();

		if($user->gid < 25){
			$canView		= Ownservice::checkAccess('account', 'view', 'account', 'all');
			if (!$canView) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		// Create the view
		$view = & $this->getView('account', 'html');

		// Get/Create the model
		$model = & $this->getModel('Account');

		// Push the model into the view (as default)
		$view->setModel($model, true);

		// Set the layout
		$view->setLayout('default');

		// Display the view
		$view->display();
	}

	/**
	 * Method to get form  edit customer account
	 *
	 * @return void
	 * @since 1.0
	 */
	function edit(){

		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken( 'request' ) or jexit( 'Invalid Token' );

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();

		if($user->gid < 25) {

			$canEdit	= Ownservice::checkAccess('account', 'edit', 'account', 'all');
			if (!$canEdit) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		// Create the view
		$view = & $this->getView('account', 'html');

		// Get/Create the model
		$model = & $this->getModel('Account');

		// Push the model into the view (as default)
		$view->setModel($model, true);

		// Set the layout
		$view->setLayout('form');

		// Display the view
		$view->display();

	}

	/**
	* Method to add an custom account
	*
	* @access	public
	* @since	1.0
	*/
	function add() {

		global $mainframe;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();

		if($user->gid < 25) {
			$canAdd		= Ownservice::checkAccess('account', 'add', 'account', 'all');
			if (!$canAdd) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		// Create the view
		$view = & $this->getView('account', 'html');

		// Get/Create the model
		$model = & $this->getModel('Account');

		// Push the model into the view (as default)
		$view->setModel($model, true);

		// Set the layout
		$view->setLayout('form');

		// Display the view
		$view->display();
	}

	/**
	 * Logic to save customer account
	 * 
	 * @access public
	 * @return void
	*/
	function save(){

		//Check for request forgeries
		JRequest::checkToken() or jexit('Invalid Token');

		$app = & JFactory::getApplication();

		// Get vars
		$post = JRequest::get( 'post' );

		$model = $this->getModel('Account');

		$type = '';

		if($model->store($post)){
			$msg = JText::_('OWNSERVICE_ACCOUNT_SAVED');
		} else {
			$type = 'error';
			$msg = JText::_('OWNSERVICE_ERROR_SAVING_ACCOUNT');
		}

		$link = JRoute::_('index.php?option=com_ownservice&view=accounts');
		
		$this->setRedirect($link, $msg, $type);
	}

	/**
	 * Logic to delete account
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	*/
	function delete(){

		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken('request') or jexit( 'Invalid Token' );

		$user	= & JFactory::getUser();
		$app	= & JFactory::getApplication();

		if( $user->gid <25 ) {
			$canDelete		= Ownservice::checkAccess('account', 'delete', 'account', 'all');
			if (!$canDelete) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		//get var
		$id = JRequest::getVar('id',0, 'post', 'int');

		$model=& $this->getModel('Accounts');
		if ($model->delete($id)) {
			$msg = JText::_('OWNSERVICE_ACCOUNT_DELETED');
		} else {
			$msg = JText::_( 'OWNSERVICE_ACCOUNT_FAILED' );
		}

		$link = JRoute::_('index.php?option=com_ownservice&view=accounts');

		$this->setRedirect( $link, $msg );
	}
	
	/**
	 * Method to fetch the members form
	 * 
	 * @since 1.5
	 */
	function viewmembers()
	{
		// Check for request forgeries
		JRequest::checkToken('request') or jexit( 'Invalid Token' );

		$user =&JFactory::getUser();

		//header('Content-type: application/json');
		@ob_end_clean();
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Cache-Control: no-cache");
		header("Pragma: no-cache");
		//header("Content-type:text/json");

		$model 			=  $this->getModel('Account');
		$memberobjs 	=  $model->getmembers(JRequest::getVar('q'));
		$array = array();
		echo "[";
		foreach($memberobjs as $member) {
			$array[] = "{\"id\":\"".$member->id."\",\"name\":\"".$member->username.' '.$member->name."\"}";
		}
		echo implode(",", $array);
		echo "]";
		exit;
	}

	/**
	 * Method to check expire of user
	 * 
	 * @access public
	 * @return json string
	 * @since 1.5
	 */
	 function checkexpire()
	 {
	 	// Check for request forgeries
		JRequest::checkToken('request') or jexit( 'Invalid Token' );

		$id  	= JRequest::getVar('id', 0 , 'post', 'int');
		
		$model 	=  $this->getModel('Account');
		
		$user	= $model->getUser($id);
		
		$curentdate	= mktime(0, 0, 0, date('m'), date('d'), date('Y'));
		list($edy, $edm, $edd)	= explode('-', $user->expire_date);
		$expiredate = mktime(0, 0, 0, $edm, $edd, $edy);
		
		$days = ($expiredate - $curentdate) / 86400;		
		
		//header('Content-type: application/json');
		@ob_end_clean();
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Cache-Control: no-cache");
		header("Pragma: no-cache");
		//header("Content-type:text/json");
		
		echo "{\"days\":\"".$days."\", \"expiredate\":\"".$user->expire_date."\",\"id\":\"".$user->id."\"}";
		exit();
	 }

	/**
	 * logic for cancel an action
	 *
	 * @access public
	 * @return void
	 * @since 1.0
	 */
	function cancel()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$app = & JFactory::getApplication();

		$link = JRoute::_('index.php?option=com_ownservice&view=accounts');

		$this->setRedirect( $link );
	}
}